//
//  MixedFiltersViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/24.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "MixedFiltersViewController.h"

@interface MixedFiltersViewController ()

@end

@implementation MixedFiltersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    /**
     *  上一节我们学习了滤镜的使用，但是你可能会有这样一个疑问：滤镜可不可以混合使用？ 答案是肯定的，滤镜是可以混合的，因此这一节我主要讲解混合滤镜的使用，我们以CIHueAdjust(饱和度) 和CIPixellate(马赛克)为例
     */
    
    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageWithName:@"filter.jpg"]];
    // 1、创建滤镜1
    CIFilter *filter1 = [CIFilter filterWithName:@"CIPixellate"];
    [filter1 setDefaults];
    [filter1 setValue:ciImage forKey:kCIInputImageKey];
    [filter1 setValue:@(8) forKey:kCIInputScaleKey]; // 取值范围1~100
    
    // 2、取出滤镜1加工过后的图片
    CIImage *outputImage1 = [filter1 outputImage];
    // 3、将outputImage1作为作为滤镜2的输入源
    CIFilter *filter2 = [CIFilter filterWithName:@"CIHueAdjust"];
    [filter2 setDefaults];
    [filter2 setValue:outputImage1 forKey:kCIInputImageKey];
    [filter2 setValue:@(M_PI_2) forKey:kCIInputAngleKey]; // 取值-2 *M_PI~2 * M_PI
    // 4、取出滤镜2加工过后的图片
    CIImage *outputImage2 = [filter2 outputImage];
    // 5、创建coreImage上下文
//    CIContext *ciContext = [CIContext contextWithOptions:nil];
    CIContext *ciContext = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:kCIContextUseSoftwareRenderer]];//CPU渲染

    // 6、运用coreImage上下文创造CGImageRef
    CGImageRef imageRef = [ciContext createCGImage:outputImage2 fromRect:[outputImage2 extent]];
    
    UIImageView *showImageV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 250, 150)];
    showImageV.image = [UIImage imageWithCGImage:imageRef];
    [self.view addSubview:showImageV];
    
}



@end
